package com.lxmes.hr.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcAbnormalAttend;
import com.lxmes.hr.domain.NcPieceworkMoneyDetail;
import com.lxmes.hr.domain.NcPieceworkParam;
import com.lxmes.hr.mapper.NcPieceworkMoneyDetailMapper;
import com.lxmes.procontrol.domain.NcProOrderInfoBg;
import com.lxmes.procontrol.domain.NcProOrderInfoBgVo;
import com.lxmes.procontrol.mapper.NcProOrderInfoBgMapper;
import com.lxmes.procontrol.mapper.NcProProcessControlMapper;
import com.lxmes.procontrol.service.INcProOrderInfoBgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcPieceworkStaMapper;
import com.lxmes.hr.domain.NcPieceworkSta;
import com.lxmes.hr.service.INcPieceworkStaService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 计件统计Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class NcPieceworkStaServiceImpl implements INcPieceworkStaService
{
    @Resource
    private NcPieceworkStaMapper ncPieceworkStaMapper;

    @Resource
    private NcPieceworkMoneyDetailMapper ncPieceworkMoneyDetailMapper;

    @Resource
    private NcProOrderInfoBgMapper ncProOrderInfoBgMapper;

    @Resource
    protected Validator validator;

    /**
     * 查询计件统计
     *
     * @param id 计件统计主键
     * @return 计件统计
     */
    @Override
    public NcPieceworkSta selectNcPieceworkStaById(Long id)
    {
        return ncPieceworkStaMapper.selectNcPieceworkStaById(id);
    }

    /**
     * 查询计件统计列表
     *
     * @param ncPieceworkSta 计件统计
     * @return 计件统计
     */
    @Override
    public List<NcPieceworkSta> selectNcPieceworkStaList(NcPieceworkSta ncPieceworkSta)
    {
        return ncPieceworkStaMapper.selectNcPieceworkStaList(ncPieceworkSta);
    }

    /**
     * 新增计件统计
     *
     * @param ncPieceworkSta 计件统计
     * @return 结果
     */
    @Override
    public int insertNcPieceworkSta(NcPieceworkSta ncPieceworkSta)
    {

        ncPieceworkSta.setCreateTime(DateUtils.getNowDate());
        return ncPieceworkStaMapper.insertNcPieceworkSta(ncPieceworkSta);
    }

    /**
     * 修改计件统计
     *
     * @param ncPieceworkSta 计件统计
     * @return 结果
     */
    @Override
    public int updateNcPieceworkSta(NcPieceworkSta ncPieceworkSta)
    {
        ncPieceworkSta.setUpdateTime(DateUtils.getNowDate());
        return ncPieceworkStaMapper.updateNcPieceworkSta(ncPieceworkSta);
    }

    /**
     * 批量删除计件统计
     *
     * @param ids 需要删除的计件统计主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkStaByIds(Long[] ids)
    {
        return ncPieceworkStaMapper.deleteNcPieceworkStaByIds(ids);
    }

    /**
     * 删除计件统计信息
     *
     * @param id 计件统计主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkStaById(Long id)
    {
        return ncPieceworkStaMapper.deleteNcPieceworkStaById(id);
    }

    @Override
    public String imporIteminfo(List<NcPieceworkSta> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPieceworkSta org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcPieceworkSta(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工工号 " + org.getJobNo() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getJobNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional
    public int jjstatistics(int type) {

        String startTime ="";
        String endTime = "";
        if(type==1){
            //获取上个月时间
            String month = DateUtils.getLastMonth();
            String day = DateUtils.LastDayOfPreviousMonth();
            ncPieceworkStaMapper.deleteNcPieceworkStaByPieceworkDate(month);
             startTime = month+"01 00:00:00";
             endTime = day+" 23:59:59";
        }else{
            String date = DateUtils.getDate();
            ncPieceworkStaMapper.deleteNcPieceworkStaByPieceworkDate(date);
//             startTime = date+" 00:00:00";
//             endTime = date+" 23:59:59";
            startTime = "2023-04-07 00:00:00";
            endTime = "2023-04-07 23:59:59";
        }

        List<NcProOrderInfoBgVo> ncProOrderInfoBgs = ncProOrderInfoBgMapper.selectBgInfoCount(startTime, endTime);
        for(NcProOrderInfoBgVo bg : ncProOrderInfoBgs){
            NcPieceworkSta pieceworkSta = new NcPieceworkSta();
            pieceworkSta.setPieceworkDate(DateUtils.parseDate(startTime.split(" ")[0]));
            pieceworkSta.setGcCode(bg.getFactoryCode());
            pieceworkSta.setGcName(bg.getFactoryName());
            pieceworkSta.setName(bg.getEmpName());
            pieceworkSta.setJobNo(bg.getEmpNum());
            pieceworkSta.setDepartment(bg.getDepartment());
            pieceworkSta.setBranch(bg.getBranch());
            pieceworkSta.setPGroup(bg.getPGroup());
            pieceworkSta.setJobs(bg.getJobs());
            pieceworkSta.setProLine(bg.getLineCode());
            pieceworkSta.setGwCode(bg.getGwCode());
            pieceworkSta.setGwName(bg.getGwName());
            pieceworkSta.setOrderNo(bg.getOrderNo());
            pieceworkSta.setItemName(bg.getProName());
            pieceworkSta.setPieceworkUnit("KG");//这里得查询计件参数表
            pieceworkSta.setBgNum(bg.getBgNum());
            pieceworkSta.setXlzl(bg.getXlzl());
            pieceworkSta.setBgzl(bg.getXlzl().multiply(bg.getBgNum()));
            pieceworkSta.setItemCode(bg.getDyProCode());
            pieceworkSta.setCreateTime(DateUtils.getNowDate());

            ncPieceworkStaMapper.insertNcPieceworkSta(pieceworkSta);
            NcPieceworkMoneyDetail ncPieceworkMoneyDetail = new NcPieceworkMoneyDetail();
            BeanUtils.copyProperties(pieceworkSta,ncPieceworkMoneyDetail);

            //查询计件参数
            List<NcPieceworkParam> ncPieceworkParams = selectJjParam(bg);
            ncPieceworkMoneyDetail.setPieceworkPrice(ncPieceworkParams.get(0).getPieceworkPrice());
            ncPieceworkMoneyDetail.setPieceworkNum(pieceworkSta.getBgNum());
            ncPieceworkMoneyDetail.setXlzl(bg.getXlzl().multiply(bg.getBgNum()));

            BigDecimal multiply = bg.getXlzl().multiply(pieceworkSta.getBgNum()).multiply(ncPieceworkParams.get(0).getPieceworkPrice());
            if(multiply==null){
                multiply = BigDecimal.valueOf(0);
            }
            ncPieceworkMoneyDetail.setPieceworkGz(multiply);
            ncPieceworkMoneyDetailMapper.insertNcPieceworkMoneyDetail(ncPieceworkMoneyDetail);
        }
        return 1;
    }

    public List<NcPieceworkParam> selectJjParam(NcProOrderInfoBgVo bg){
        List<NcPieceworkParam> ncPieceworkParams;
        NcPieceworkParam ncPieceworkParam = new NcPieceworkParam();
        ncPieceworkParam.setItemCode(bg.getDyProCode());
        ncPieceworkParams = ncPieceworkMoneyDetailMapper.selectNcPieceworkParamList(ncPieceworkParam);
        if(ncPieceworkParams.size()==0){
            NcPieceworkParam ncPieceworkParam2 = new NcPieceworkParam();
            ncPieceworkParam2.setJobNo(bg.getEmpNum());
            ncPieceworkParams = ncPieceworkMoneyDetailMapper.selectNcPieceworkParamList(ncPieceworkParam2);
            if(ncPieceworkParams .size()==0){
                NcPieceworkParam ncPieceworkParam3 = new NcPieceworkParam();
                ncPieceworkParam3.setDepartment(bg.getDepartment());
                ncPieceworkParams = ncPieceworkMoneyDetailMapper.selectNcPieceworkParamList(ncPieceworkParam3);
                if(ncPieceworkParams.size()==0){
                    NcPieceworkParam ncPieceworkParam4 = new NcPieceworkParam();
                    ncPieceworkParam4.setGcCode(bg.getFactoryCode());
                    ncPieceworkParams = ncPieceworkMoneyDetailMapper.selectNcPieceworkParamList(ncPieceworkParam4);
                    if(ncPieceworkParams.size()==0){
                        throw new RuntimeException("请维护计件参数信息！");
                    }
                }
            }
        }
        return ncPieceworkParams;
    }
}



